package com.cbgolf.oa.views;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseView;
import com.cbgolf.oa.entity.OrderDetailsBean;
import com.cbgolf.oa.presenter.IPresenterImp;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.TypeUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.widget.ListViewNew;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.abslistview.CommonAdapter;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/5.
 */

public class AccountDetailsView extends BaseView {

  //
  @ViewInject(R.id.topBack_ll)
  private View topBack;
  @ViewInject(R.id.title_tv)
  private TextView titleTv;
  //
  @ViewInject(R.id.account_details_checkin_time_tv)
  private TextView timeTv;
  @ViewInject(R.id.account_details_cardnum_tv)
  private TextView cardNumTv;
  @ViewInject(R.id.account_details_userid_tv)
  private TextView userIdTv;
  @ViewInject(R.id.account_details_qiubao_num_tv)
  private TextView bagNumTv;
  @ViewInject(R.id.account_details_state_tv)
  private TextView stateTv;
  @ViewInject(R.id.account_details_name_tv)
  private TextView nameTv;
  @ViewInject(R.id.account_details_chuwugun_num_tv)
  private TextView storeNumTv;
  @ViewInject(R.id.account_details_paymoney_time_tv)
  private TextView payTimeTv;
  //
  @ViewInject(R.id.account_details_listview1)
  private ListViewNew listView1;
  @ViewInject(R.id.account_details_money_tv)
  private TextView moneyTv;
  @ViewInject(R.id.account_details_hole_tv)
  private TextView holeNumTv;
  @ViewInject(R.id.account_details_paymoney_name_tv)
  private TextView payNameTv;

  @ViewInject(R.id.account_details_listview2)
  private ListViewNew listView2;
  @ViewInject(R.id.paytime_name_ll)
  private View payNameTimeView;
  /**
   * worker
   */
  private IPresenterImp worker;

  public AccountDetailsView(Activity act, IPresenterImp worker) {
    ViewUtils.autoInjectAllField(this, act);
    this.worker = worker;
    super.init(act);
  }

  @Override
  protected void setViews() {
    titleTv.setText(TextUtil.Text(worker.getActivityTitle()));

  }

  @Override
  protected void setListener() {
    topBack.setOnClickListener(v -> worker.back());
  }

  @Override
  public void getDataSuccess(Object obj) {
    OrderDetailsBean bean = (OrderDetailsBean) obj;
    if (null == bean) {
      Util.showNetError();
    } else {
      showDetails(bean);
    }
  }

  private float allMoney;

  private void showDetails(OrderDetailsBean bean) {
    timeTv.setText(DateUtil.stampToDate(bean.recordDate, DateUtil.formatYMD));
    cardNumTv.setText(TextUtil.Text(bean.consumerCardNo));
    userIdTv.setText(TextUtil.Text(bean.identityName));
    bagNumTv.setText(TextUtil.Text(bean.bagNumber));
    stateTv.setText(TextUtil.Text(getState(bean.status)));
    nameTv.setText(TextUtil.Text(bean.name));
    storeNumTv.setText(TextUtil.Text(bean.lockerNo));
    holeNumTv.setText(TextUtil.textNumFloat(bean.ballCave));

    if (!Util.listIsNull(bean.consumerItems)) {
      try {
        CommonAdapter<OrderDetailsBean.consumerItem> adapter = new CommonAdapter<OrderDetailsBean.consumerItem>(
            context,
            R.layout.item_list1, bean.consumerItems) {
          @Override
          public void convert(ViewHolder holder, OrderDetailsBean.consumerItem consumerItem) {
            holder.setText(R.id.item_list1_name_tv, TextUtil.Text(consumerItem.goodsName));
            holder.setText(R.id.item_list1_adr_tv, TextUtil.Text(consumerItem.orderPlace));
            holder.setText(R.id.item_list1_count_tv, TextUtil.Text(consumerItem.consumeNumber));
            holder.setText(R.id.item_list1_money_tv, "￥" + TextUtil.textNumFloat(consumerItem.payable));
          }
        };
        listView1.setAdapter(adapter);
        for (int i = 0; i < bean.consumerItems.size(); i++) {
          allMoney += TypeUtil.parseFloat(bean.consumerItems.get(i).payable);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    moneyTv.setText(("应付费用   ￥" + TextUtil.textNumFloat(allMoney)));
    if (payNameTimeView.getVisibility() == View.VISIBLE && !Util.listIsNull(bean.cashWays)) {
      List<OrderDetailsBean.cash.Item> list = new ArrayList<>();
      try {
        for (int i = 0; i < bean.cashWays.size(); i++) {
          list.add(bean.cashWays.get(i).items.get(0));
        }
        CommonAdapter<OrderDetailsBean.cash.Item> adapter = new CommonAdapter<OrderDetailsBean.cash.Item>(
            context,
            R.layout.item_list1, list) {
          @Override
          public void convert(ViewHolder holder, OrderDetailsBean.cash.Item bean) {
            holder.setText(R.id.item_list1_name_tv, TextUtil.Text(bean.cashWayName));
            holder.setText(R.id.item_list1_adr_tv, TextUtil.textNumFloat(bean.payFree));
            holder.setText(R.id.item_list1_count_tv, TextUtil.Text(bean.tailNumber));
            holder.setText(R.id.item_list1_money_tv, TextUtil.Text(bean.cardholder));
          }
        };
        listView2.setAdapter(adapter);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    payTimeTv.setText(DateUtil.stampToDate(bean.cashTime, DateUtil.formatYMD));
    payNameTv
        .setText(("结账人   " + TextUtil.Text(null != bean.cashPerson ? bean.cashPerson.name : "")));
  }

  private String getState(String state) {
    if (Util.isEquals("PAIED", state)) {
      payNameTimeView.setVisibility(View.VISIBLE);
      return "已结账";
    } else if (Util.isEquals("NO_PAY", state)) {
      payNameTimeView.setVisibility(View.GONE);
      return "未结账";
    }
    return null;
  }
}
